import { Global, Module } from '@nestjs/common';
import { RedisService } from './redis.service';
import Redis from 'ioredis';
import { ConfigService } from '@nestjs/config';
import { REDIS_CLIENT } from '../../../common/constants/redis.constant';

@Global()
@Module({
  providers: [
    RedisService,
    {
      provide: REDIS_CLIENT,
      inject: [ConfigService],
      async useFactory(configService: ConfigService) {
        const port = configService.get<number>('REDIS_PORT', 6379);
        const host = configService.get<string>('REDIS_HOST', '127.0.0.1');
        const db = configService.get<number>('REDIS_DB', 0);
        return new Redis(port, host, {
          db,
        });
      },
    },
  ],
  exports: [RedisService],
})
export class RedisModule {}
